using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XeoEngine
{
    class MessageBoxScreen : GameScreen
    {
        string message;
        Texture2D gradientTexture;
        Texture2D borderTexture;
        string ok;
        string cancel;
        int timeincancelbutton = -255;
        public int TimeInCancelButton
        {
            get { return timeincancelbutton; }
            set { timeincancelbutton = value; }
        }
        DateTime oldtime;
        Vector4 okpos;
        Vector4 cancelpos;
        Color okcolor;
        Color cancelcolor;
        int selectedindex;
        ButtonState bs = ButtonState.Released;
        public event EventHandler<PlayerIndexEventArgs> Accepted;
        public event EventHandler<PlayerIndexEventArgs> Cancelled;
        public MessageBoxScreen(string message)
            : this(message, Localizations.Strings.Yes, Localizations.Strings.No)
        { }
        public MessageBoxScreen(string message, string OkName)
            : this(message, OkName, Localizations.Strings.No)
        { }
        public MessageBoxScreen(string message, string OkName, string CancelName)
        {
            this.message = message;
            IsPopup = true;
            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
            this.ok = OkName;
            this.cancel = CancelName;
            oldtime = DateTime.Now;
        }

        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;
            gradientTexture = content.Load<Texture2D>("Menu/gradient");
            borderTexture = content.Load<Texture2D>("Menu/border");
            Vector2 textSize = ScreenManager.Font.MeasureString(this.message);
            Vector2 textPosition = (new Vector2(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height) - textSize) / 2;
            Vector2 oksize = ScreenManager.Font.MeasureString(ok);
            Vector2 cancelsize = ScreenManager.Font.MeasureString(cancel + timeincancelbutton.ToString());
            if (cancel == "")
                okpos.X = textPosition.X + textSize.X / 2 - oksize.X/2;
            else
                okpos.X = textPosition.X;
            okpos.Y = textPosition.Y + textSize.Y + 16;
            okpos.Z = oksize.X;
            okpos.W = oksize.Y;
            cancelpos.X = textPosition.X + textSize.X - cancelsize.X - 16;
            cancelpos.Y = textPosition.Y + textSize.Y + 16;
            cancelpos.Z = cancelsize.X;
            cancelpos.W = cancelsize.Y;
        }

        public override void HandleInput(InputState input)
        {
            if (timeincancelbutton != -255)
            {
                if (DateTime.Now.Ticks - oldtime.Ticks >= 10000000)
                {
                    timeincancelbutton--;
                    if (timeincancelbutton <= 0)
                    {
                        if (Cancelled != null)
                            Cancelled(this, new PlayerIndexEventArgs(PlayerIndex.One));
                        ExitScreen();
                    }
                    oldtime = DateTime.Now;
                }
            }
            PlayerIndex playerIndex;
            MouseState ms = Mouse.GetState();
            if (ms.X > okpos.X && ms.X < okpos.X + okpos.Z && ms.Y > okpos.Y && ms.Y < okpos.Y + okpos.W)
            {
                if (selectedindex != 0)
                {
                    selectedindex = 0;
                    ScreenManager.soundBank.PlayCue("UI_Click");
                }
                if (ms.LeftButton == ButtonState.Pressed && bs == ButtonState.Released)
                {
                    if (Accepted != null)
                        Accepted(this, new PlayerIndexEventArgs(PlayerIndex.One));
                    ExitScreen();
                }
            }
            if (cancel != "")
                if (ms.X > cancelpos.X && ms.X < cancelpos.X + cancelpos.Z && ms.Y > cancelpos.Y && ms.Y < cancelpos.Y + cancelpos.W)
                {
                    if (selectedindex != 1)
                    {
                        selectedindex = 1;
                        ScreenManager.soundBank.PlayCue("UI_Click");
                    }
                    if (ms.LeftButton == ButtonState.Pressed && bs == ButtonState.Released)
                    {
                        if (Cancelled != null)
                            Cancelled(this, new PlayerIndexEventArgs(PlayerIndex.One));
                        ExitScreen();
                    }
                }
            if (cancel != "")
            {
                if (input.IsMenuLeft(ControllingPlayer))
                {
                    selectedindex++;
                }
                if (input.IsMenuRight(ControllingPlayer))
                {
                    selectedindex--;
                }
            }
            if (selectedindex > 1) selectedindex = 0;
            if (selectedindex < 0) selectedindex = 1;
            if (selectedindex == 0)
            {
                okcolor = Color.DeepSkyBlue;
                cancelcolor = Color.LightGray;
            }
            else
            {
                okcolor = Color.LightGray;
                cancelcolor = Color.DeepSkyBlue;
            }
            if (input.IsMenuSelect(ControllingPlayer, out playerIndex))
            {
                if (selectedindex == 0)
                {
                    if (Accepted != null)
                        Accepted(this, new PlayerIndexEventArgs(playerIndex));
                    ExitScreen();
                }
                else
                {
                    if (Cancelled != null)
                        Cancelled(this, new PlayerIndexEventArgs(playerIndex));
                    ExitScreen();
                }
            }
            else if (input.IsMenuCancel(ControllingPlayer, out playerIndex))
            {
                if (Cancelled != null)
                    Cancelled(this, new PlayerIndexEventArgs(playerIndex));
                ExitScreen();
            }
            bs = ms.LeftButton;
        }
        public override void Draw(GameTime gameTime)
        {
            string canceltext;
            if (timeincancelbutton != -255)
            {
                canceltext = cancel + "(" + timeincancelbutton.ToString() + ")";
            }
            else
            {
                canceltext = cancel;
            }
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
            Vector2 textSize = font.MeasureString(message);
            Vector2 textPosition = (viewportSize - textSize) / 2;
            const int hPad = 32;
            const int vPad = 16;
            Rectangle backgroundRectangle = new Rectangle((int)textPosition.X - hPad,
                                                          (int)textPosition.Y - vPad,
                                                          (int)textSize.X + hPad * 2,
                                                          (int)textSize.Y + (int)cancelpos.W + 32 + vPad * 2);
            Color color = Color.White * TransitionAlpha;
            spriteBatch.Begin();
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);
            spriteBatch.Draw(borderTexture, new Rectangle((int)textPosition.X - hPad-5, (int)textPosition.Y - vPad, (int)textSize.X + hPad * 2+10, 10), color);
            spriteBatch.Draw(borderTexture, new Rectangle((int)textPosition.X - hPad - 5, (int)textPosition.Y - vPad + (int)textSize.Y + (int)cancelpos.W + 32 + vPad * 2, (int)textSize.X + hPad * 2 + 10, 10), color);
            spriteBatch.DrawString(font, message, textPosition, color);
            spriteBatch.DrawString(font, ok, new Vector2(okpos.X, okpos.Y), okcolor * TransitionAlpha);
            spriteBatch.DrawString(font, canceltext, new Vector2(cancelpos.X, cancelpos.Y), cancelcolor * TransitionAlpha);
            spriteBatch.End();
        }
    }
}
